<template>
<div class="app">
  <el-tag
      :key="index" v-for="(name,index) in Hobby"
      closable
      :disable-transitions="false"
      @close="handleClose(name)"
      class="tag">
    {{name}}
  </el-tag>

  <el-input
      class="input-new-tag"
      v-if="inputVisible"
      v-model="inputValue"
      ref="saveTagInput"
      size="small"
      @keyup.enter.native="handleInputConfirm"
      @blur="handleInputConfirm">
    <el-button
      slot="append"
      type="primary"

      @click="handleInputConfirm">确定</el-button>
  </el-input>
  <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>

</div>
</template>

<script>
import axios from "axios";
import {updateUser} from "@/utils/api";

export default {
  name: "HobbyComponent",
  props: {},
  data() {
    return {

      inputVisible: false,
      inputValue: ''
    }
  }, methods: {
    //同步
    async updateUser(){
      const user = this.getUser;
      user.password=''
      updateUser(user).then(res=>{
        if(res.code != 200){
          this.$message.error(res.message)
        }
      })
    },
    //删除
    handleClose(name) {

      let hobby =this.Hobby
      hobby.splice(hobby.indexOf(name), 1)
      this.Hobby = hobby
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {

      let hobby =this.Hobby
      if (this.inputValue){
          hobby.push(this.inputValue)
          this.Hobby = hobby
      }

      this.inputVisible = false;
      this.inputValue = '';
    }
  }, watch: {}, computed: {
    getUser:{
      get(){
        return this.$store.getters.getUser;
      },
      set(value){
        return this.$store.commit("setUser",value);
      }
    },
    Hobby:{
      get(){
        return this.getUser.hobby;
      },
      set(hobby){
        let user = this.getUser;
        user.hobby = hobby
        this.getUser = user;
        this.updateUser()
      }

    }
  }
}
</script>

<style scoped>
.app{
  display: flex;
  flex-wrap: wrap;
}
.tag{
  margin-top: 10px;
  margin-left: 10px;
}
.button-new-tag {
  margin-top: 10px;
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 150px;
  margin-left: 10px;
  margin-top: 10px;
  vertical-align: bottom;
}
</style>